package com.zengqingfa.rocketmq.demo;

import com.zengqingfa.rocketmq.sender.MqDemo01Sender;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @fileName: HelloController
 * @author: zengqf3
 * @date: 2021-4-2 22:51
 * @description:
 */
@RestController
@RequestMapping("/hello")
@Slf4j
public class HelloController {

    @Value("${rocketmq.topic.demo01}")
    private String topic;

    @Value("${rocketmq.tag.demo01}")
    private String tag;

    @Autowired
    private MqDemo01Sender mqDemo01Sender;

    @RequestMapping("send")
    public String hello() throws Exception {
        List<Message> msgs = new ArrayList<>();
        for (int i = 0; i < 1; i++) {
            Message msg = new Message(topic, tag, ("Hello RocketMQ  " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            msgs.add(msg);
        }
        // 调用客户端发送消息
        mqDemo01Sender.send(msgs);
        return "SUCCESS";
    }
}
